//
//  WCNRiskAccessmentRecordsCell.m
//  WCN
//
//  Created by 陈武琦 on 16/3/7.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNRiskAccessmentRecordsCell.h"
#import "Masonry.h"
#import "WCImageView +setImage.h"
#import "WCNRiskAccessmentRecordsModel.h"

@implementation WCNRiskAccessmentRecordsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpControls];
        self.backgroundColor = kAppBg;
    }

    return self;
}

- (void)setUpControls {

    _leftImage = [[UIImageView alloc] init];
    [_leftImage setImage:[UIImage imageNamed:@"CaseCell_BlueHeader"]];
    [_leftImage setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.contentView addSubview:_leftImage];
    
    _date = [self setUpLabel];
    _date.font = [UIFont systemFontOfSize:11];
    _date.textColor = kTxtColor;
    [self.contentView addSubview:_date];
    
    _backBorderView = [[UIView alloc] init];
    //[_backBorderView setTranslatesAutoresizingMaskIntoConstraints:NO];
    _backBorderView.backgroundColor = [UIColor whiteColor];
    _backBorderView.layer.borderColor = kBorderColor.CGColor;
    _backBorderView.layer.borderWidth = 0.5;
    [self.contentView addSubview:_backBorderView];

    
    _accessmentResult = [self setUpLabel];
    _accessmentResult.font = [UIFont systemFontOfSize:16];
    _accessmentResult.numberOfLines = 0;
    [_backBorderView addSubview:_accessmentResult];
    
    _advice = [self setUpLabel];
    _advice.font = [UIFont systemFontOfSize:14];
    _advice.textColor = kTxtColor;
    _advice.numberOfLines = 0;
    
    [_backBorderView addSubview:_advice];
    
    _changeFrequency = [self setUpLabel];
    _changeFrequency.font = [UIFont systemFontOfSize:14];
    _changeFrequency.textColor = kTxtColor;
    _changeFrequency.numberOfLines = 0;
    [_backBorderView addSubview:_changeFrequency];

    _rightImage = [[UIImageView alloc] init];
    [_backBorderView addSubview:_rightImage];
    _rightImage.contentMode = UIViewContentModeScaleAspectFill;
    _rightImage.layer.cornerRadius = 22.0;
    _rightImage.layer.masksToBounds = YES;
    
    
    [_advice setContentHuggingPriority:UILayoutPriorityRequired
                               forAxis:UILayoutConstraintAxisVertical];
    [_advice setContentCompressionResistancePriority:UILayoutPriorityRequired
                                             forAxis:UILayoutConstraintAxisVertical];
    
    
    [_changeFrequency setContentHuggingPriority:UILayoutPriorityDefaultHigh
                                        forAxis:UILayoutConstraintAxisVertical];
    [_changeFrequency setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh
                                                      forAxis:UILayoutConstraintAxisVertical];
 
    [self setConstrants];
    
}
- (void)setConstrants{
    
    [_leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView.mas_left).offset(5);
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(10, 10));
        
        
    }];
    
    
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_leftImage.mas_right).offset(5);
        make.right.equalTo(self.contentView.mas_right).offset(-5);
        make.top.equalTo(_leftImage.mas_top);
        make.bottom.equalTo(_backBorderView.mas_top);
    }];
    
    
    
    [_backBorderView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(25, 7.5, 0, 7.5));
    }];
    
    
    [_accessmentResult mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_backBorderView.mas_left).offset(10);
        make.right.equalTo(_rightImage.mas_left).offset(-5);
        make.top.equalTo(_backBorderView.mas_top);
        make.height.equalTo(@50);
    }];
    
    [_rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_backBorderView.mas_right).offset(-10);
        make.top.equalTo(_backBorderView.mas_top).offset(5);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    
    [_advice mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_backBorderView.mas_left).offset(10);
        make.right.equalTo(_backBorderView.mas_right).offset(-5);
       // make.top.equalTo(_accessmentResult.mas_bottom);
        make.bottom.equalTo(_changeFrequency.mas_top);
    }];
    
    [_changeFrequency mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_backBorderView.mas_left).offset(10);
        make.right.equalTo(_backBorderView.mas_right).offset(-5);
       // make.top.equalTo(_advice.mas_bottom);
        make.bottom.equalTo(_backBorderView.mas_bottom);
        // make.height.equalTo(@30);
        
    }];
    
}

- (void)setModel:(WCNRiskAccessmentRecordsModel *)model {

    _model = model;
    self.date.text = model.updateDate;
    
    //处理为空的情况
    NSMutableAttributedString *attributeString;
    if (!model.sumScore) {
        model.sumScore = @"";
        attributeString = [[NSMutableAttributedString alloc] initWithString:[model.name stringByAppendingFormat:@": %@",model.sumScore]];
    }else {
        attributeString = [[NSMutableAttributedString alloc] initWithString:[model.name stringByAppendingFormat:@": %@ 分",model.sumScore]];
    }
    
    if (!model.column1Description) {
        model.column1Description = @"";
    }
    
    if (!model.column2Description) {
        model.column2Description = @"";
    }
    
    [attributeString addAttribute:NSForegroundColorAttributeName value:kTxtColor range:NSMakeRange([model.name length], [attributeString length] - [model.name length] - 2)];
    
    
    _accessmentResult.attributedText = attributeString;
    _advice.text = [model.column1Name stringByAppendingString:[NSString stringWithFormat:@": %@",model.column1Description]];
    _changeFrequency.text = [model.column2Name stringByAppendingString:[NSString stringWithFormat:@": %@",model.column2Description]];
    [self.rightImage wsjSetImageWithUrl:model.avatarFileId withPlaceImageName:@"Nurse_default"];

}


- (UILabel *)setUpLabel{
    
    UILabel *label = [[UILabel alloc] init];
    //[label setTranslatesAutoresizingMaskIntoConstraints:NO];
    //[self.contentView addSubview:label];
    return label;

}



- (void)awakeFromNib {
    // Initialization code
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
